草庐IT

react-router-dom V6

全部标签

javascript - this.setState 是否在 react 中返回 promise

我使我的componentWillMount()异步。现在我可以将await与setState一起使用。示例代码如下:componentWillMount=async()=>{const{fetchRooms}=this.propsawaitthis.setState({})fetchRooms()}所以这里的问题是this.setState返回promise因为我可以使用await吗?编辑当我放置await时,它会按顺序运行1,2,3当我删除await时,它会运行1,3,2??componentWillMount=async()=>{const{fetchRooms}=this.pr

javascript - React 路由器不显示浏览器历史记录

我正在从中学习tutorial但我不断收到此错误:'react-router'doesnotcontainanexportnamed'browserHistory'.有react-router的文件是这样的:importReactfrom'react';importReactDOMfrom'react-dom';import{Router,browserHistory}from'react-router';importroutesfrom'./routes';ReactDOM.render(,document.getElementById('root'));

javascript - React hooks - 清除超时和间隔的正确方法

我不明白为什么当我使用setTimeout函数时,我的React组件开始无限运行console.log。一切正常,但PC开始变得非常滞后。有人说超时功能改变了我的状态和重新渲染组件,设置了新的计时器等等。现在我需要了解如何清除它是对的。exportdefaultfunctionLoading(){//ifdatafetchingisslow,after1seciwillshowsomeloadinganimationconst[showLoading,setShowLoading]=useState(true)lettimer1=setTimeout(()=>setShowLoadin

javascript - 如何知道焦点何时在 react native textInput 中丢失?

如何知道焦点何时在reactnative文本输入中丢失?例如,我想在用户触摸textInput外部并失去焦点时执行一个操作。 最佳答案 在TextInput上使用onBlur属性例子console.log("focusreceived")}onBlur={()=>console.log("focuslost")}/>Docs 关于javascript-如何知道焦点何时在reactnativetextInput中丢失?,我们在StackOverflow上找到一个类似的问题:

javascript - 测试 angularjs ui-router go() 方法

我有一个Controller,它从$scope获取一个值并将它发送到不同的状态:controllers.controller('SearchController',['$scope','$state','$stateParams',function($scope,$state,$stateParams){$scope.search=function(){$stateParams.query=$scope.keyword;$state.go('search',$stateParams);};}]);我不确定如何对这种搜索方法进行单元测试。我如何验证go方法是否已被调用或执行某种when(

javascript - EmberJS : How to transition to a router from a controller's action

我有一个Action:{{actioncreatetarget="controller"}}我像这样针对绑定(bind)的Controller(而不是路由器):App.AddBoardController=Ember.Controller.extendcreate:->App.store.createRecordApp.Board,{title:@get"boardName"}App.store.commit()//TODO:Redirecttoroute如何从Controller操作重定向回路由? 最佳答案 使用transitio

javascript - 如何使用 Jest 和/或 Enzyme 获取嵌套在 React 组件中的元素的属性?

我想测试一下图像是否已正确加载到React应用程序中。我决定检查嵌套在React组件中的img元素的src属性。我想使用Jest测试框架,如果需要,还想使用Enzyme测试实用程序。通过深入研究浅层React测试组件的Object.keys,我得出了以下解决方案。我不确定的行用星号表示。importReactfrom'react';import{shallow}from'enzyme';importAppfrom'./App';it('shouldhavethelogoimage',()=>{constapp=shallow();constimg=app.find('img');con

javascript - React 函数/Hooks 组件上的 componentDidMount 等价物?

有没有办法通过钩子(Hook)在React函数式组件中模拟componentDidMount? 最佳答案 对于稳定版的hooks(ReactVersion16.8.0+)对于componentDidMountuseEffect(()=>{//Yourcodehere},[]);对于componentDidUpdateuseEffect(()=>{//Yourcodehere},[yourDependency]);对于componentWillUnmountuseEffect(()=>{//componentWillUnmountre

javascript - 在 React.js 中将 key 传递给 child

我正在浏览一个关于tutsplus的React教程,它有点旧,而且代码不能像最初编写的那样工作。实际上,我完全同意这一点,因为它迫使我更加独立地学习,但是我花了一段时间来解决一个我无法弄清楚的错误。该错误包括无法传递对象键,这会阻止我的程序更新正确对象的状态。如果您想运行此代码并查看它的运行情况,首先是存储库:https://github.com/camerow/react-voteit我有一个看起来像这样的子组件:varFeedItem=React.createClass({vote:function(newCount){console.log("Votingon:",this.pr

javascript - 如何在功能组件 [React] 中设置 displayName

我知道有时需要设置displayName,尤其是在处理生产构建时。我想知道如何使用我的功能组件进行设置-是否可能/允许?这是我的类组件中的内容:constMyComponent=React.createClass({displayName:'HeyHey',render:function(){console.log(this.displayName);}});如何在无状态组件中做同样的事情? 最佳答案 displayName的文档说ThedisplayNamestringisusedindebuggingmessages.Usual